# MP3 Voiceprint Comparison Test (1:1 and 1:N)

$baseUrl = "http://localhost:8082/api/voiceprint"
$audioFile = "test_real_mp3.mp3"
$groupId = "test_group_real_mp3"
$testFeatureId = "test_real_mp3_20250912_233114"  # Use the feature we just created

Write-Host "=== MP3 Voiceprint Comparison Test ==="
Write-Host "Audio File: $audioFile"
Write-Host "Group ID: $groupId"
Write-Host "Test Feature ID: $testFeatureId"
Write-Host ""

# Check if audio file exists
if (-not (Test-Path $audioFile)) {
    Write-Host "Error: Audio file not found: $audioFile"
    exit 1
}

# Get file size
$fileSize = (Get-Item $audioFile).Length
Write-Host "File size: $fileSize bytes"
Write-Host ""

# Test 1: 1:1 Comparison
Write-Host "=== Test 1: 1:1 Voiceprint Comparison ==="
Write-Host "Comparing the same audio file against the registered feature..."

# Create multipart form data for 1:1 comparison
$boundary = [System.Guid]::NewGuid().ToString()
$LF = "`r`n"

$bodyLines = @(
    "--$boundary",
    "Content-Disposition: form-data; name=`"featureId`"",
    "",
    $testFeatureId,
    "--$boundary",
    "Content-Disposition: form-data; name=`"audioFile`"; filename=`"$audioFile`"",
    "Content-Type: audio/mpeg",
    ""
)

$bodyText = $bodyLines -join $LF
$bodyBytes = [System.Text.Encoding]::UTF8.GetBytes($bodyText + $LF)

# Read audio file
$audioBytes = [System.IO.File]::ReadAllBytes($audioFile)

# Create closing boundary
$closingBoundary = [System.Text.Encoding]::UTF8.GetBytes($LF + "--$boundary--" + $LF)

# Combine all parts
$requestBody = New-Object System.Collections.Generic.List[byte]
$requestBody.AddRange($bodyBytes)
$requestBody.AddRange($audioBytes)
$requestBody.AddRange($closingBoundary)

Write-Host "Request body size: $($requestBody.Count) bytes"

# Add groupId parameter for 1:1 comparison
$bodyLines = @(
    "--$boundary",
    "Content-Disposition: form-data; name=`"groupId`"",
    "",
    $groupId,
    "--$boundary",
    "Content-Disposition: form-data; name=`"featureId`"",
    "",
    $testFeatureId,
    "--$boundary",
    "Content-Disposition: form-data; name=`"audioFile`"; filename=`"$audioFile`"",
    "Content-Type: audio/mpeg",
    ""
)

$bodyText = $bodyLines -join $LF
$bodyBytes = [System.Text.Encoding]::UTF8.GetBytes($bodyText + $LF)

# Read audio file
$audioBytes = [System.IO.File]::ReadAllBytes($audioFile)

# Create closing boundary
$closingBoundary = [System.Text.Encoding]::UTF8.GetBytes($LF + "--$boundary--" + $LF)

# Combine all parts
$requestBody = New-Object System.Collections.Generic.List[byte]
$requestBody.AddRange($bodyBytes)
$requestBody.AddRange($audioBytes)
$requestBody.AddRange($closingBoundary)

Write-Host "Request body size: $($requestBody.Count) bytes"

try {
    $compareUrl = "$baseUrl/compare/one-to-one"
    
    $webClient = New-Object System.Net.WebClient
    $webClient.Headers.Add("Content-Type", "multipart/form-data; boundary=$boundary")
    
    $response = $webClient.UploadData($compareUrl, "POST", $requestBody.ToArray())
    $responseText = [System.Text.Encoding]::UTF8.GetString($response)
    
    Write-Host "1:1 Comparison successful!"
    Write-Host "Response: $responseText"
    
} catch {
    Write-Host "1:1 Comparison failed: $($_.Exception.Message)"
    Write-Host "Detailed error: $($_.Exception.InnerException.Message)"
} finally {
    if ($webClient) {
        $webClient.Dispose()
    }
}

Write-Host ""
Write-Host "=== Test 2: 1:N Voiceprint Comparison ==="
Write-Host "Searching for the audio file in the group..."

# Create multipart form data for 1:N comparison
$boundary2 = [System.Guid]::NewGuid().ToString()

$bodyLines2 = @(
    "--$boundary2",
    "Content-Disposition: form-data; name=`"groupId`"",
    "",
    $groupId,
    "--$boundary2",
    "Content-Disposition: form-data; name=`"audioFile`"; filename=`"$audioFile`"",
    "Content-Type: audio/mpeg",
    ""
)

$bodyText2 = $bodyLines2 -join $LF
$bodyBytes2 = [System.Text.Encoding]::UTF8.GetBytes($bodyText2 + $LF)

# Create closing boundary
$closingBoundary2 = [System.Text.Encoding]::UTF8.GetBytes($LF + "--$boundary2--" + $LF)

# Combine all parts
$requestBody2 = New-Object System.Collections.Generic.List[byte]
$requestBody2.AddRange($bodyBytes2)
$requestBody2.AddRange($audioBytes)
$requestBody2.AddRange($closingBoundary2)

Write-Host "Request body size: $($requestBody2.Count) bytes"

try {
    $searchUrl = "$baseUrl/compare/one-to-many"
    
    $webClient2 = New-Object System.Net.WebClient
    $webClient2.Headers.Add("Content-Type", "multipart/form-data; boundary=$boundary2")
    
    $response2 = $webClient2.UploadData($searchUrl, "POST", $requestBody2.ToArray())
    $responseText2 = [System.Text.Encoding]::UTF8.GetString($response2)
    
    Write-Host "1:N Search successful!"
    Write-Host "Response: $responseText2"
    
} catch {
    Write-Host "1:N Search failed: $($_.Exception.Message)"
    Write-Host "Detailed error: $($_.Exception.InnerException.Message)"
} finally {
    if ($webClient2) {
        $webClient2.Dispose()
    }
}

Write-Host ""
Write-Host "=== All Tests Completed ==="